package falstad;

import java.awt.Container;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * Class implements a wrapper for the user input handled by the Maze class. 
 * It is shared by MazeApplication and MazeApp to talk to the Maze.
 *
 * This code is refactored code from Maze.java by Paul Falstad, www.falstad.com, Copyright (C) 1998, all rights reserved
 * Paul Falstad granted permission to modify and use code for teaching purposes.
 * Refactored by Peter Kemper
 */
public class ManualDriverKeyListener implements KeyListener {

	Container parent ;
	Robot robot ;
	
	ManualDriverKeyListener(Container parent, Robot robot){
		this.parent = parent ;
		this.robot = robot;
	}
	
	/**
	 * Translates keyboard input to the corresponding characters for the Maze.keyDown method
	 */
	@Override
	public void keyPressed(KeyEvent arg0) {
		//System.out.println("1: Communicating key: " + arg0.getKeyText(arg0.getKeyCode()) + " with key char: " + arg0.getKeyChar() + " with code: " + arg0.getKeyCode());
		int key = arg0.getKeyChar() ;
		int code = arg0.getKeyCode() ;
		
	//	System.out.println("Key: "+ key);
		//System.out.println("Code: "+code);


		if (KeyEvent.CHAR_UNDEFINED == key)
		{
			if ((KeyEvent.VK_0 <= code && code <= KeyEvent.VK_9)||(KeyEvent.VK_A <= code && code <= KeyEvent.VK_Z))
				key = code ;
			if (KeyEvent.VK_ESCAPE == code)
				robot.escape();
			if (KeyEvent.VK_UP == code)
				try {
					robot.move(1, true);
				} catch (HitObstacleException e) {
					e.printStackTrace();
				}
			if (KeyEvent.VK_DOWN == code)
				try {
					robot.move(1, false);
				} catch (HitObstacleException e) {
					e.printStackTrace();
				}
			if (KeyEvent.VK_LEFT == code)
				try {
					robot.rotate(Constants.LEFT_ROTATE);
				} catch (UnsupportedArgumentException e) {
					//e.printStackTrace();
				}
			if (KeyEvent.VK_RIGHT == code)
				try {
					robot.rotate(Constants.RIGHT_ROTATE);
				} catch (UnsupportedArgumentException e) {
					//e.printStackTrace();
				}
		}
		//robot.printCurrentInfo();
		//System.out.println("Calling keydown with " + key) ;
		
	//	System.out.println("Position: " + robot.getCurrentPosition()[0] + ", " + robot.getCurrentPosition()[1]);
		parent.repaint();
	}
	@Override
	public void keyReleased(KeyEvent arg0) {
		// nothing to do
		
	}
	@Override
	public void keyTyped(KeyEvent arg0) {
		// NOTE FOR THIS TYPE OF EVENT IS getKeyCode always 0, so Escape etc is not recognized	
		// this is why we work with keyPressed
	}

}
